*Creates national partisan wave variable for forecasting model.  

clear
version 15.1
set varabbrev off, permanently

cd C:\Users\User\Dropbox\08_FORECAST\NationalVars\Batch20181029

global datezzz 20181029
global midtermlosszzz 004_Nat_Midterm_Loss2014_10_22
global natincomezzz 003income20181029
global natvarszzz 005_NationalVarsQuick20181029reworked

import excel $midtermlosszzz.xlsx, firstrow
gen even=1-(mod(year,2))
drop if year<1964|year>2018
keep if even==1
rename mid_pen midpen
rename prez_party prezparty
drop even mid_elect
save temp, replace

clear
import excel $natvarszzz.xlsx, firstrow

gen approval=(approve/(approve+disapprove))*100

rename dem2ptyadults congintent
keep year approval congintent

merge 1:1 year using temp
drop _merge
erase temp.dta

merge 1:1 year using $natincomezzz
drop if _merge==2
drop _merge

replace income=income*100

*reverse midterm penalty, because it's set up now to reflect a "1" representing a penalty for the Democrats.
replace midpen=midpen*-1

*For income and approval, assume that the average would be electorally equivalent if either a Democrat or Republican is in the Whitehouse.  
foreach string in income approval {
sum `string'
local aaa=r(mean)
gen `string'2=(`string'-`aaa')*prezparty
}

*Z-score all variables.  
foreach string in income2 approval2 midpen congintent {
sum `string'
local aaa=r(mean)
local bbb=r(sd)
gen `string'z=(`string'-`aaa')/`bbb'
}

*Downweight presidential approval by a factor of 2 when a president is leaving the whitehouse.  
*WHEN BEFORE AUGUST 8: Downweight presidential approval in the case of 1974, as Nixon left the Whitehouse on August 8, and Ford's approval rating was very high immediately after taking office, although it declined pretty rapidly.  It was obviously a lot higher than Nixon's, who was in office until early August.  So if it's before August 8, add 1974 to the following list, if after August 8, leave 1974 out of the following list.  
gen leaving=year==1968|year==1988|year==2000|year==2008|year==2016

gen wave=((congintentz*4)+(midpenz*3)+(approval2z*2)+income2z)/10 if leaving==0
replace wave=((congintentz*4)+(midpenz*3)+approval2z+income2z)/9 if leaving==1

sort wave

save 007natvars$datezzz, replace

keep year wave

*tsset year
*gen lag=wave[_n-1]
*gen dif=wave-lag
*gen lag2=wave[_n-2]
*gen dif2=wave-lag2
*sort dif
*sort dif2
*browse
*drop lag dif lag2 dif2

sort year

save 008wave$datezzz, replace

export delimited 008wave$datezzz.csv, replace

